package com.yi.servicemap.remote;

import com.yi.internalcommon.constant.AmapConfigConstants;
import com.yi.internalcommon.dto.ResponseResult;
import com.yi.internalcommon.request.PointDTO;
import com.yi.internalcommon.request.PointRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@Service
@Slf4j
public class PointClient {
    @Value("${amap.key}")
    private String amapKey;

    @Value("${amap.sid}")
    private String amapSid;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 轨迹點上傳
     * @param pointRequest
     * @return
     */
    public ResponseResult upload(PointRequest pointRequest){
        //拼装请求url
        StringBuilder url=new StringBuilder();
        url.append(AmapConfigConstants.POINT_UPLOAD);
        url.append("?");
        url.append("key="+amapKey);
        url.append("&");
        url.append("sid="+amapSid);
        url.append("&");
        url.append("tid="+pointRequest.getTid());
        url.append("&");
        url.append("trid="+pointRequest.getTrid());
        url.append("&");
        url.append("points=");
        url.append("%5B");
        for (PointDTO point : pointRequest.getPoints()) {
            url.append("%7B");
            url.append("%22location%22");
            url.append("%3A");
            url.append("%22"+point.getLocation()+"%22");
            url.append("%2C");
            url.append("%22locatetime%22");
            url.append("%3A");
            url.append(point.getLocatetime());
            url.append("%7D");
        }
        url.append("%5D");

        log.info("上传位置请求"+url.toString());
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URI.create(url.toString()), null, String.class);
        log.info("上传位置响应"+stringResponseEntity.getBody());
        return ResponseResult.success();
    }
}
